/**
 * 
 */

$(function(){
	
	//查询
	$("#btnSearch").click(function(){
		var formSerialize = $("#form").serializeObject();
		var v = JSON.stringify(formSerialize);
		$("#v").val(encodeURIComponent(v));
		form.submit();
	});
	
	
});

function add() {
	
	$.layer({
	    type: 2,
	    shade: [0],
	    fix: false,
	    title: '添加商家',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/adv/partner/add?type=init"},
	    area: ['400px' , '420px'],
	    close: function(index){
	    }
	});
}

function addType() {
	var title = $("#title").val();
	var description = $("#description").val();
	
	if (null == title || "" == title) {
		layer.tips("标题不能为空", $("#title") , {guide: 3, time: 3,more:true});
		return false;
	}
	
	var imgUrl = $("#imgUrl").val();
	if (null == imgUrl || "" == imgUrl) {
		layer.tips("图标不能为空", $("#imgUrl") , {guide: 3, time: 3,more:true});
		return false;
	}
	
	
	//ajax file upload
	$.ajaxFileUpload({
		url : global.rootPath + "/adv/partner/add?type=add",
		fileElementId:"logoFile",
		dataType: 'json',
		
		data : {
			title : encodeURIComponent(title),
			description : encodeURIComponent(description)
		},
		success: function(data,status) {
			//parent.layer.msg(data.msg, 2);
			parent.location.reload();
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
//			debugger
//			if (data.success) {
//				parent.layer.msg(data.msg, 2);
//				refresh();
//    			var index = parent.layer.getFrameIndex(window.name);
//    			parent.layer.close(index);
//    			
//			} else {
//            	//parent.layer.msg(data.msg, 2);
//			}
		},
		error:function(data,status,e) {
			parent.layer.msg(data.msg, 3);
		}
	});
	
}

function update(id) {
	
	if (null == id || "" == id ) {
		layer.msg("操作异常", 3);
		return false;
	}
	
	$.layer({
	    type: 2,
	    shade: [0],
	    fix: false,
	    title: '修改商家信息',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/adv/partner/update?type=init&id="+id},
	    area: ['400px' , '420px'],
	    close: function(index){
	    }
	});
}

function updateType() {
	var title = $("#title").val();
	var description = $("#description").val();
	var id = $("#id").val();
	
	if (null == title || "" == title) {
		layer.tips("标题不能为空", $("#title") , {guide: 3, time: 3,more:true});
		return false;
	}
	
	var imgUrl = $("#imgUrl").val();
	//if (null == imgUrl || "" == imgUrl) {
	//	layer.tips("分类图标不能为空", $("#imgUrl") , {guide: 3, time: 3,more:true});
	//	return false;
	//}
	
	
	//ajax file upload
	$.ajaxFileUpload({
		url : global.rootPath + "/adv/partner/update?type=update",
		fileElementId:"logoFile",
		dataType: 'json',
		
		data : {
			title : encodeURIComponent(title),
			description : encodeURIComponent(description),
			id : id,
			imgUrl : imgUrl
		},
		success: function(data,status) {
			//parent.layer.msg(data.msg, 2);
			parent.location.reload();
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
//			if (data.success) {
//				parent.layer.msg(data.msg, 2);
//				refresh();
//    			var index = parent.layer.getFrameIndex(window.name);
//    			parent.layer.close(index);
//    			
//			} else {
//            	parent.layer.msg(data.msg, 2);
//			}
		},
		error:function(data,status,e) {
			//parent.layer.msg(data.msg, 3);
		}
	});
	
}



function cancel(id){
	
	$.layer({
	    shade: [0],
	    area: ['auto','auto'],
	    dialog: {
	        msg: '是否确定注销该商家信息？',
	        btns: 2,                    
	        type: 4,
	        btn: ['确定','取消'],
	        yes: function(){
	        	security.ajax({
	        		url : global.rootPath + "/adv/partner/cancel",
	        		data : {
	        			id : id
	        		},
	        		beforeSend : function(){
	        			security.showLoading();
	        		},
	        		success : function(code,msg) {
	        			if (msg == "") msg = "注销成功";
	        			security.showSuccess(msg,refresh);
	        		},
	        		error : function(message) {
	        			security.showError(message);
	        		}
	        	});
	        }, no: function(){
	        }
	    }
	});
	
}

function closeIframe() {
	var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

function refresh() {
	var formSerialize = $("#form").serializeObject();
	var v = JSON.stringify(formSerialize);
	$("#v").val(encodeURIComponent(v));
	form.submit();
}